package cn.jboa.service.impl;

import cn.jboa.common.Constants;
import cn.jboa.dao.CheckResultDao;
import cn.jboa.dao.ClaimVoucherDao;
import cn.jboa.dao.EmployeeDao;
import cn.jboa.entity.BizCheckResult;
import cn.jboa.entity.BizClaimVoucher;
import cn.jboa.service.CheckResultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service("checkResultService")
public class CheckResultServiceImpl implements CheckResultService {

    @Autowired
    @Qualifier("checkResultDao")
    private CheckResultDao checkResultDao;
    @Autowired
    @Qualifier("claimVoucherDao")
    private ClaimVoucherDao claimVoucherDao;
    @Autowired
    @Qualifier("empDao")
    private EmployeeDao employeeDao;

    @Override
    public void saveCheckResult(BizCheckResult checkResult) {
        checkResult.setCheckTime(new Date());
        checkResultDao.save(checkResult);

        BizClaimVoucher claimVoucher=claimVoucherDao.get(checkResult.getClaimId());
        claimVoucher.setModifyTime(checkResult.getCheckTime());
        this.updateClaimVoucherStatus
                (checkResult.getCheckEmployee().getSysPosition().getNameCn(),checkResult.getResult(),claimVoucher);
    }

    protected void updateClaimVoucherStatus(String position,String checkResult,BizClaimVoucher claimVoucher){

        // 通过
        if (Constants.CHECKRESULT_PASS.equals(checkResult)) {
            // FM
            if (Constants.POSITION_FM.equals(position)) {
                // >=5000
                if (claimVoucher.getTotalAccount() >= 5000) {
                    claimVoucher.setStatus(Constants.CLAIMVOUCHER_APPROVING);
                    claimVoucher.setNextDeal(employeeDao.getGeneralManager());
                }
                // < 5000
                else {
                    claimVoucher.setStatus(Constants.CLAIMVOUCHER_APPROVED);
                    claimVoucher.setNextDeal(employeeDao.getCashier());
                }
            }
            // GM
            else if (Constants.POSITION_GM.equals(position)) {
                claimVoucher.setStatus(Constants.CLAIMVOUCHER_APPROVED);
                claimVoucher.setNextDeal(employeeDao.getCashier());
            }
            // Cashier
            else if (Constants.POSITION_CASHIER.equals(position)) {
                claimVoucher.setStatus(Constants.CLAIMVOUCHER_PAID);
                claimVoucher.setNextDeal(null);
            }
        }
        // 拒绝
        else if (Constants.CHECKRESULT_REJECT.equals(checkResult)) {
            claimVoucher.setStatus(Constants.CLAIMVOUCHER_TERMINATED);
            claimVoucher.setNextDeal(null);
        }
        // 打回
        else if (Constants.CHECKRESULT_BACK.equals(checkResult)) {
            claimVoucher.setStatus(Constants.CLAIMVOUCHER_BACK);
            claimVoucher.setNextDeal(claimVoucher.getCreator());
        }
    }

    public CheckResultDao getCheckResultDao() {
        return checkResultDao;
    }

    public void setCheckResultDao(CheckResultDao checkResultDao) {
        this.checkResultDao = checkResultDao;
    }

    public ClaimVoucherDao getClaimVoucherDao() {
        return claimVoucherDao;
    }

    public void setClaimVoucherDao(ClaimVoucherDao claimVoucherDao) {
        this.claimVoucherDao = claimVoucherDao;
    }

    public EmployeeDao getEmployeeDao() {
        return employeeDao;
    }

    public void setEmployeeDao(EmployeeDao employeeDao) {
        this.employeeDao = employeeDao;
    }
}
